package edu.gdin.ecommerce.action;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import edu.gdin.ecommerce.model.Account;
import edu.gdin.ecommerce.model.Card;
import edu.gdin.ecommerce.model.CardIssuer;
import edu.gdin.ecommerce.model.Customer;
import edu.gdin.ecommerce.model.Inventory;
import edu.gdin.ecommerce.model.User;
import edu.gdin.ecommerce.service.IAccountService;
import edu.gdin.ecommerce.service.ICardBatchService;
import edu.gdin.ecommerce.service.ICardIssuerService;
import edu.gdin.ecommerce.service.ICardService;
import edu.gdin.ecommerce.service.ICustomerService;
import edu.gdin.ecommerce.service.IInventoryService;
import edu.gdin.ecommerce.util.DateUtil;
import edu.gdin.ecommerce.util.Pager;

@Component("accountAction")
@Scope("prototype")
public class AccountAction extends ActionSupport {
	
	private Account account = new Account();
	private Integer customerId;
	private String batchNo;
	private String inventoryNo;
	private String cardType;
	private Integer currentPage;
	
	//供查询用
	private String customerName;
	private String cardNo;
	private String accountName;
	private Integer useType;
	private Pager pager;
	private String filePath;
	private String importFile;
	private String msg;
	
	
	//供更新
	private Integer accountId;
	
	private IInventoryService inventoryService;
	private ICardBatchService cardBatchService;
	private ICustomerService customerService;
	private IAccountService accountService;
	private ICardService cardService;
	private ICardIssuerService cardIssuerService;
	private List<CardIssuer> issuers;
	
	private String operTime1;
	private String operTime2;
	
	private String result;
	
	public String getOperTime1() {
		return operTime1;
	}
	public void setOperTime1(String operTime1) {
		this.operTime1 = operTime1;
	}
	public String getOperTime2() {
		return operTime2;
	}
	public void setOperTime2(String operTime2) {
		this.operTime2 = operTime2;
	}
	public Account getAccount() {
		return account;
	}
	public void setAccount(Account account) {
		this.account = account;
	}
	public Integer getCustomerId() {
		return customerId;
	}
	public void setCustomerId(Integer customerId) {
		this.customerId = customerId;
	}
	public String getBatchNo() {
		return batchNo;
	}
	public void setBatchNo(String batchNo) {
		this.batchNo = batchNo;
	}
	public String getInventoryNo() {
		return inventoryNo;
	}
	public void setInventoryNo(String inventoryNo) {
		this.inventoryNo = inventoryNo;
	}
	public IInventoryService getInventoryService() {
		return inventoryService;
	}
	
	public String getCardType() {
		return cardType;
	}
	public void setCardType(String cardType) {
		this.cardType = cardType;
	}
	@Resource
	public void setInventoryService(IInventoryService inventoryService) {
		this.inventoryService = inventoryService;
	}
	
	public ICardBatchService getCardBatchService() {
		return cardBatchService;
	}
	
	@Resource
	public void setCardBatchService(ICardBatchService cardBatchService) {
		this.cardBatchService = cardBatchService;
	}
	
	public ICustomerService getCustomerService() {
		return customerService;
	}
	
	@Resource
	public void setCustomerService(ICustomerService customerService) {
		this.customerService = customerService;
	}
	
	public IAccountService getAccountService() {
		return accountService;
	}
	
	@Resource
	public void setAccountService(IAccountService accountService) {
		this.accountService = accountService;
	}
	
	
	public ICardService getCardService() {
		return cardService;
	}
	
	public ICardIssuerService getCardIssuerService() {
		return cardIssuerService;
	}
	
	@Resource
	public void setCardIssuerService(ICardIssuerService cardIssuerService) {
		this.cardIssuerService = cardIssuerService;
	}
	@Resource
	public void setCardService(ICardService cardService) {
		this.cardService = cardService;
	}
	
	public List<CardIssuer> getIssuers() {
		return issuers;
	}
	public void setIssuers(List<CardIssuer> issuers) {
		this.issuers = issuers;
	}
	
	public String getCustomerName() {
		return customerName;
	}
	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}
	public String getCardNo() {
		return cardNo;
	}
	public void setCardNo(String cardNo) {
		this.cardNo = cardNo;
	}
	
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public String getAccountName() {
		return accountName;
	}
	public void setAccountName(String accountName) {
		this.accountName = accountName;
	}
	
	public Integer getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}
	
	public Pager getPager() {
		return pager;
	}
	public void setPager(Pager pager) {
		this.pager = pager;
	}
	
	public String getFilePath() {
		return filePath;
	}
	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}
	public String getImportFile() {
		return importFile;
	}
	public void setImportFile(String importFile) {
		this.importFile = importFile;
	}
	public Integer getUseType() {
		return useType;
	}
	public void setUseType(Integer useType) {
		this.useType = useType;
	}
	
	public Integer getAccountId() {
		return accountId;
	}
	public void setAccountId(Integer accountId) {
		this.accountId = accountId;
	}
	
	
	
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	
	// ----------  methods  -----------
	
	
	public String addAccountAction(){
		
		Map<String, Object> session = (Map<String, Object>) ActionContext.getContext().getSession();
		User user = (User) session.get("user");
		
		Inventory inventory  = inventoryService.queryInvenoryById(inventoryNo);
		Customer customer = customerService.getCustomerById(customerId);
		
		Card card = new Card();
		card.setCardNo(inventory.getInventoryNo());
		card.setCardDesc(inventory.getInventoryDesc());
		card.setCardIssuer(inventory.getCardIssuer());
		card.setCreateTime(DateUtil.now());
		card.setCreateUser(user.getUsername());
		card.setMaterialType(inventory.getMaterialType());
		card.setPassword(inventory.getPassword());
		card.setUseType(Integer.parseInt(cardType));
		card.setBatchNo(inventory.getBatchNo());
		card.setStatus(2);
		card.setMark(1);
		
		account.setStatus(1);
		account.setCreateTime(DateUtil.now());
		account.setCustomer(customer);
		account.setBalance(0D);
		account.setCurrency(1);
		account.setCreditLeft(account.getCreditAmount());
		if(accountService.addAccount(account)){
			card.setAccount(account);
			if(this.cardService.addCard(card)){
				setIssuers(this.cardIssuerService.getAllIssuer());
				inventory.setStatus(4);
				if(this.inventoryService.updateInvenory(inventory));
					return "accountList";
			}
		}
		return "accountAdd";
	}
	
	
	public String batchAddAccount() {
		Map<String, Object> session = (Map<String, Object>) ActionContext.getContext().getSession();
		User user = (User) session.get("user");
		try {
			this.accountService.batchAddAccount(this.batchNo, this.useType, this.account, user);
		} catch (Exception e) {
			//e.printStackTrace();
			this.addFieldError("msg", e.getMessage());
			return "preBatchAddAccount";
		}
		return "batchAddAccount";
	}
	
	
	public String impportAddFile() {
		try {
			this.filePath = this.accountService.importAddFile();
		} catch (Exception e) {
			this.addFieldError("msg", "模板下载失败!");
			return "impportAddFileFail";
		}
		return "impportAddFile";
	}
	
	
	public String importAddAccount() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		User user = (User) session.get("user");
		try {
			this.msg = this.accountService.importAddAccount(this.importFile, user);
		} catch (Exception e) {
			this.addFieldError("msg", e.getMessage());
			return "preImportAdd";
		}
		return "importAddAccount";
	}
	
	
	public String queryAccountAction(){
		Integer cPage = currentPage == null || currentPage < 1 ? 1 : currentPage;
		setPager( accountService.queryAccount(cardNo, accountName, useType, customerName, operTime1,operTime2, cPage));
		if(this.pager.getList().size() == 0) {
			this.addFieldError("msg", "没找到符合条件的账户！");
		}
		return "accountList";
	}
	
	
	public String preUpdateAction(){
		setAccount(accountService.getAccountById(accountId));
		return "accountUpdate";
	}
	
	
	public String modifyAccountAction(){
		
		Account a = accountService.getAccountById(accountId);
		a.setBalanceWarn(account.getBalanceWarn());
		a.setCreditAmount(account.getCreditAmount());
		a.setName(account.getName());
		a.setScore(account.getScore());
		a.setSingleExpense(account.getSingleExpense());
		a.setStatus(account.getStatus());
		a.setUpdateTime(DateUtil.now());
		
		if(this.accountService.updateAccount(a)){
			setMsg("success");
			return "accountList";
		}
		else{
			setMsg("faild");
			return preUpdateAction();
		}
	}
	
	
	public String mofifyAccountSingleExpenseAction(){
		Account a = accountService.getAccountById(accountId);
		a.setSingleExpense(account.getSingleExpense());
		if(this.accountService.updateAccount(a)){
			result="success";
		}
		else{
			result="faild";
		}
		return SUCCESS;
	}
	
	public String getAccountRecordAction(){
		Integer cpg = currentPage;
		if( currentPage==null ||currentPage==0 )
			cpg = 1;
		setAccount(accountService.getAccountById(accountId));
		setPager(accountService.getAccountRecoredById(accountId, cpg));
		if(pager.getList().size()==0)
			this.addFieldError("msg", "暂无数据！");
		return "recordList";
	}
}
